package cn.itcast.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Xiao
 * @version 1.0
 * @description 配置死信交换机队列
 * @date 2021/11/19
 */
@Configuration
public class TTLMessageConfig {
    private static final String TTL_EXCHANGE = "ttl.direct";
    private static final String TTL_QUEUE = "ttl.queue";
    private static final String TTL_TEST_KEY = "ttl.test";
    private static final Integer TTL_TIME = 10000;

    private static final String DL_EXCHANGE = "dl.direct";
    private static final String DL_TEST_KEY = "dl.test";

    @Bean
    public DirectExchange ttlDirectExchange() {
        return new DirectExchange(TTL_EXCHANGE);
    }

    @Bean
    public Queue ttlQueue() {
        return QueueBuilder
                .durable(TTL_QUEUE)
                .ttl(TTL_TIME)
                .deadLetterExchange(DL_EXCHANGE)
                .deadLetterRoutingKey(DL_TEST_KEY)
                .build();
    }

    @Bean
    public Binding ttlQueueBinding() {
        return BindingBuilder.bind(ttlQueue()).to(ttlDirectExchange()).with(TTL_TEST_KEY);
    }
}
